﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DB_NH
{
    using NHibernate;
    using NHibernate.Criterion;
    using NHibernate.Cfg;
    using NHibernate.Cfg.MappingSchema;
    using NHibernate.Context;
    using NHibernate.Driver;
    using NHibernate.Dialect;
    using NHibernate.Mapping.ByCode.Conformist;
    using NHibernate.Mapping.ByCode;
    using NHibernate.Tool.hbm2ddl;

    using DB_NH.DB_Map.MYD;
    using DB_NH.DB_Map.YWDF;
    using DB_NH.DB_Map.YWGZ;
    using DB_NH.DB_Map.ywxt;
    using DB_NH.DB_Map.ZJK;
    using DB_NH.DB_Map.ZJCQ;

    public class SessionFactory
    {
        public static ISessionFactory GetCurrentFactory()
        {
            if (sessionFactory == null)
            {
                sessionFactory = CreateSessionFactory();
            }

            return sessionFactory;
        }


        public static string OutputPath = @"D:\\bhm";
        public static string OutputDbFile = OutputPath + @"\\data.db";
        public static string OutputSQLFile = OutputPath + @"\\sql.txt";
        public static string OutputMAPFile = OutputPath + @"\\map.txt";

        private static ISessionFactory CreateSessionFactory()
        {
            //设置数据库连接字符串
            //string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["data_ywxt"].ConnectionString;

            var configure = new Configuration();
            configure.SessionFactoryName("ywxt2");

            var mapper = GetMappings();

            var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
            configure.AddMapping(hbmMapping);

            //配置数据库
            SchemaMetadataUpdater.QuoteTableAndColumns(configure);

#if DEBUG
            //*
            //首次运行，创建数据库，输出MAP映射文件、数据库创建脚本
            //File.WriteAllText(OutputMAPFile, hbmMapping.AsString());

            //输出数据库创建脚本，但不创建数据库
           // new SchemaExport(configure).SetOutputFile(OutputSQLFile).Create(true, false);

            //输出数据库创建脚本，且创建数据库
            //new SchemaExport(configure).SetOutputFile(OutputSQLFile).Create(true, true);
            //*/
#endif
            try
            {

                //建立SessionFactory
                sessionFactory = configure.BuildSessionFactory();

            }
            catch (Exception ex)
            {

                throw ex;
            }
            return sessionFactory;
        }

        private static ISessionFactory sessionFactory
        {
            get;
            set;
        }

        /// <summary>
        /// 设置mapping
        /// </summary>
        /// <returns></returns>
        public static ModelMapper GetMappings()
        {
            var mapper = new ModelMapper();

            mapper.AddMapping<DB_Map.MYD.XMCZWT_Map>();
            mapper.AddMapping<DB_Map.MYD.XMMYD_Table_Map>();
            mapper.AddMapping<DB_Map.MYD.XMNBWJPJ_Map>();

            //业务盖章申请
            mapper.AddMapping<DB_Map.YWGZ.wf_ywgz_Map>();
            mapper.AddMapping<DB_Map.YWGZ.wf_gz_Map>();
            mapper.AddMapping<DB_Map.YWGZ.wf_bsgz_Map>();
            mapper.AddMapping<DB_Map.YWGZ.wf_zbgz_Map>();

            //业务订房流程
            mapper.AddMapping<DB_Map.YWDF.wf_ywdf_Map>();
            mapper.AddMapping<DB_Map.YWDF.ywdf_hotel_Map>();
            mapper.AddMapping<DB_Map.YWDF.ywdf_order_detail_Map>();
            mapper.AddMapping<DB_Map.YWDF.ywdf_order_Map>();
            mapper.AddMapping<DB_Map.YWDF.ywdf_room_Map>();

            //业务系统
            mapper.AddMapping<DB_Map.ywxt.t_attachment_Map>();
            mapper.AddMapping<DB_Map.ywxt.t_project_Map>();
            mapper.AddMapping<DB_Map.ywxt.t_sys_department_Map>();
            mapper.AddMapping<DB_Map.ywxt.t_sys_user_Map>();
            mapper.AddMapping<DB_Map.ywxt.wf_act_mail_Map>();
            mapper.AddMapping<DB_Map.ywxt.wf_action_douser_Map>();
            mapper.AddMapping<DB_Map.ywxt.wf_action_Map>();
            mapper.AddMapping<DB_Map.ywxt.wf_maillist_send_atts_Map>();
            mapper.AddMapping<DB_Map.ywxt.wf_maillist_send_Map>();
            mapper.AddMapping<DB_Map.ywxt.wf_node_atts_Map>();
            mapper.AddMapping<DB_Map.ywxt.wf_node_Map>();
            mapper.AddMapping<DB_Map.ywxt.wf_type_Map>();
            mapper.AddMapping<DB_Map.ywxt.fy_detail_Map>();
            mapper.AddMapping<DB_Map.ywxt.t_sys_finance_type_Map>();

            #region 投标人信息
            mapper.AddMapping<DB_Map.ywxt.ref_ProjectAndEnterprise_Map>();
            mapper.AddMapping<DB_Map.ywxt.Enterprise_Map>();
            mapper.AddMapping<DB_Map.ywxt.EnterpriseUser_Map>();
            mapper.AddMapping<DB_Map.ywxt.EnterpriseLinkUser_Map>();
            #endregion

            #region 项目购买记录
            mapper.AddMapping<DB_Map.ywxt.ProjectBuyRecord.ProjectBuyRecord_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectBuyRecord.ProjectDocument_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectBuyRecord.ProjectDocumentBuyRecord_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectBuyRecord.ProjectFinanceRecord_Map>();

            mapper.AddMapping<DB_Map.ywxt.TenderOffer.Map_TenderOfferCfg>();
            mapper.AddMapping<DB_Map.ywxt.TenderOffer.Map_TenderOfferCfgDetail>();
            mapper.AddMapping<DB_Map.ywxt.TenderOffer.Map_TenderOffer>();
            mapper.AddMapping<DB_Map.ywxt.TenderOffer.Map_TenderOfferDetail>();
            #endregion

            //项目参与人
            mapper.AddMapping<DB_Map.ywxt.Project.ProjectUsers_Map>();

            //项目所需序列号
            mapper.AddMapping<DB_Map.ywxt.Map_ProjectSerialCode>();

            //项目计划安排
            mapper.AddMapping<DB_Map.ywxt.Project.ProjectPlan_Map>();
            mapper.AddMapping<DB_Map.ywxt.Project.RefProjectAndPlan_Map>();

            //保证金
            mapper.AddMapping<DB_Map.ywxt.ProjectBZJ.ProjectBZJ_S_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectBZJ.ProjectBZJ_T_Map>();

            //服务费
            mapper.AddMapping<DB_Map.ywxt.ProjectBZJ.ProjectFwfSQ_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectBZJ.ProjectFwfSQDetail_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectBZJ.ProjectFWF_S_Map>();

            //保证金退保申请
            mapper.AddMapping<DB_Map.ywxt.ProjectBZJ.ProjectTBSQ_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectBZJ.ProjectTBSQDetail_Map>();

            //开标记录
            mapper.AddMapping<DB_Map.ywxt.ProjectOpening.ProjectOpeningRecord_Map>();

            mapper.AddMapping<DB_Map.ywxt.ProjectOpening.ProjectOpenData_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectOpening.ProjectOpenDataDetail_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectOpening.ProjectOpenDataRow_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectOpening.ProjectOpenRow_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectOpening.ProjectOpenRow_Templete_Map>();

            mapper.AddMapping<DB_Map.ywxt.ProjectOpening.ProjectOpenPageSetting_Map>();

            //开标签到
            mapper.AddMapping<DB_Map.ywxt.ProjectOpening.ProjectOpeningQDB_Map>();
            //开标签到打印记录
            mapper.AddMapping<DB_Map.ywxt.ProjectOpening.ProjectOpen_qdb_print_Map>();

            //评标记录
            mapper.AddMapping<DB_Map.ywxt.ProjectEvaluation.ProjectEvaluationRecord_Map>();

            //项目新建项申请
            mapper.AddMapping<DB_Map.ywxt.ProjectNew.map_ProjectNewSQ>();

            //项目归档
            mapper.AddMapping<DB_Map.ywxt.ProjectArchiving.ProjectArchiving_Map>();

            //专家抽取
            mapper.AddMapping<DB_Map.ZJCQ.ZJ_extract_bak_map>();
            mapper.AddMapping<DB_Map.ZJCQ.ZJ_extract_detail_bak_map>();
            mapper.AddMapping<DB_Map.ZJCQ.ZJ_extract_map>();
            mapper.AddMapping<DB_Map.ZJCQ.ZJ_extract_detail_map>();

            //专家库
            mapper.AddMapping<DB_Map.ZJK.ZJ_category_map>();
            mapper.AddMapping<DB_Map.ZJK.ZJ_expert_attachment_map>();
            mapper.AddMapping<DB_Map.ZJK.ZJ_expert_map>();

            //费用报账
            mapper.AddMapping<DB_Map.FYBZ.wf_fybz_map>();
            mapper.AddMapping<DB_Map.FYBZ.wf_fybz_detail_map>();

            //评委费
            mapper.AddMapping<DB_Map.PWF.wf_pwf_map>();
            mapper.AddMapping<DB_Map.PWF.wf_pwf_detail_map>();

            //自定义简单那流程
            mapper.AddMapping<DB_Map.SimpleFlow.map_ApprovalRecord>();
            mapper.AddMapping<DB_Map.SimpleFlow.map_ApprovalAttachment>();
            mapper.AddMapping<DB_Map.SimpleFlow.map_ApprovalRecordType>();
            mapper.AddMapping<DB_Map.SimpleFlow.map_ApprovalNode>();
            mapper.AddMapping<DB_Map.SimpleFlow.map_ApprovalToDo>();
            mapper.AddMapping<DB_Map.SimpleFlow.map_ApprovalMail>();
            mapper.AddMapping<DB_Map.SimpleFlow.map_ApprovalToDoUserRole>();
            mapper.AddMapping<DB_Map.SimpleFlow.map_ApprovalDataItem>();
            //公告审批流程记录
            mapper.AddMapping<DB_Map.SimpleFlow.SpNode_Map>();

            //考勤
            mapper.AddMapping<DB_Map.KaoQin.map_kqChuChai>();
            mapper.AddMapping<DB_Map.KaoQin.map_kqQingJia>();
            mapper.AddMapping<DB_Map.KaoQin.map_kqWaiChu>();
            mapper.AddMapping<DB_Map.KaoQin.map_kqCheckPoint>();
            mapper.AddMapping<DB_Map.KaoQin.map_kqCheckPoint_tmpl>();
            mapper.AddMapping<DB_Map.KaoQin.map_kqData>();
            mapper.AddMapping<DB_Map.KaoQin.map_kqRecord>();
            mapper.AddMapping<DB_Map.KaoQin.map_kqUser>();

            //项目奖金申请
            mapper.AddMapping<DB_Map.ywxt.ProjectJJ.map_ProjectJJSQ>();

            //项目公告信息
            mapper.AddMapping<DB_Map.ywxt.ProjectAnnouncement.ProjectAnnouncementType_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectAnnouncement.ProjectAnnouncement_Map>();

            //项目奖金表
            mapper.AddMapping<DB_Map.Finance.map_JJBForProject>();
            //项目保证金回执
            mapper.AddMapping<DB_Map.Finance.map_BZJReceiptForProject>();

            //会议室
            mapper.AddMapping<DB_Map.MeetingRoom.MapForMeetingRoom>();

            //新旧系统跳转传递参数的存储表
            mapper.AddMapping<DB_Map.ywxt.old_2_new_Map>();

            //工作单
            mapper.AddMapping<DB_Map.WorkBill.WorkBillType_Map>();
            mapper.AddMapping<DB_Map.WorkBill.WorkBill_map>();
            mapper.AddMapping<DB_Map.WorkBill.WorkBillEvaluate_map>();
            mapper.AddMapping<DB_Map.WorkBill.WorkBillFlow_Map>();

            //项目汇总信息
            mapper.AddMapping<DB_Map.ywxt.ProjectSummary.map_ProjectSummaryInfo>();

            //附件信息
            mapper.AddMapping<DB_Map.Attachment.MapAttachment>();
            mapper.AddMapping<DB_Map.Attachment.MapAttachmentData>();

            //流程信息
            mapper.AddMapping<DB_Map.SimpleFlow.MapForsfNode>();
            mapper.AddMapping<DB_Map.SimpleFlow.MapForsfRecord>();
            mapper.AddMapping<DB_Map.SimpleFlow.MapForsfToDo>();
            mapper.AddMapping<DB_Map.SimpleFlow.MapForsfType>();
            mapper.AddMapping<DB_Map.SimpleFlow.MapForsfTypeStep>();
            mapper.AddMapping<DB_Map.SimpleFlow.MapForsfTypeStepRole>();


            #region 外网映射
            //注册信息临时表
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_Register_Temp>();

            //注册信息正式表
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_Register_Enterprise>();

            //国家代码
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_Standard_Countrycode>();

            //经济类型代码
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_Standard_EconomicType>();

            //行政区域代码
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_Standard_Region>();

            //行业类型代码
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_Standard_IndustryTypes>();

            //联系人代码
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_Register_Link_User>();

            //列表信息
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_Information>();

            //列表信息类型
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_Information_Type>();

            //项目信息
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_Project>();

            //部门信息
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_Department>();

            //用户信息
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_Employee>();

            //用户信息
            mapper.AddMapping<DB_Map.ywxt.ProjectArchiving.Map_ProjectArchivingWeb>();

            //门户网站内容发布
            mapper.AddMapping<DB_Map.gxjdWebSite.Map_PortalArticle>();
            #endregion

            #region 发文审批盖章流程（自定义）
            mapper.AddMapping<DB_Map.StampApprove.map_StampApproveFlowEvaluationType>();
            mapper.AddMapping<DB_Map.StampApprove.map_StampApproveFlow>();
            mapper.AddMapping<DB_Map.StampApprove.map_StampApproveFlowEvaluationDetail>();
            mapper.AddMapping<DB_Map.StampApprove.map_StampApproveFlowAttachments>();
            #endregion

            #region 项目归档附件
            mapper.AddMapping<DB_Map.ywxt.ProjectArchiving.ProjectArchiving_Map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectArchiving.ProjectArchivingAttachment_Map>();
            #endregion

            #region 广西电网开标记录
            mapper.AddMapping<DB_Map.ywxt.ProjectOpenGXDW.ProjectGXDWOpenRecord_map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectOpenGXDW.ProjectGXDWOpenDetail_map>();
            mapper.AddMapping<DB_Map.ywxt.ProjectOpenGXDW.ProjectGXDWOpenConfig_map>();
            #endregion

            #region 保证金
            //投标保证金子账户
            mapper.AddMapping<DB_Map.ywxt.TenderBond.Map_BZJ_Sub_Account>();
            //投标保证金入账及退保明细记录
            mapper.AddMapping<DB_Map.ywxt.TenderBond.Map_BZJ_Tender_Bond_Record>();

            mapper.AddMapping<DB_Map.ywxt.TenderBond.Map_TenderBondBackApproval>();
            mapper.AddMapping<DB_Map.ywxt.TenderBond.Map_TenderBondBackApprovalDetail>();
            #endregion

            #region 监控录像下载
            mapper.AddMapping<DB_Map.VideoDownload.map_VideoDownLoadRecord>();
            mapper.AddMapping<DB_Map.VideoDownload.map_VideoCamera>();
            mapper.AddMapping<DB_Map.VideoDownload.map_VideoServer>();
            mapper.AddMapping<DB_Map.VideoDownload.map_VideoRoom>();
            #endregion

            #region 外网门户投标人注册
            mapper.AddMapping<DB_Map.Bidder.MapBidder>();
            #endregion

            #region 各大网站新闻
            mapper.AddMapping<DB_Map.News.News_Map>();
            #endregion

            #region 企业资质

            mapper.AddMapping<DB_Map.Qualification.MapQualification>();
            mapper.AddMapping<DB_Map.Qualification.MapQualificationDetails>();

            #endregion

            #region 网站数据爬虫
            mapper.AddMapping<DB_Map.gxykdx.MapGxzfcg>();
            #endregion
            return mapper;
        }
    }
}
